# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=opencolorio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=2.4.2
pkgrel=3
pkgdesc="A color management framework for visual effects and animation (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://opencolorio.org/"
msys2_repository_url='https://github.com/AcademySoftwareFoundation/OpenColorIO'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-imath"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-minizip-ng"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-pystring"
             "${MINGW_PACKAGE_PREFIX}-python"
             $( [[ "${CARCH}" != "aarch64" ]] \
                || echo "${MINGW_PACKAGE_PREFIX}-sse2neon" )
             "${MINGW_PACKAGE_PREFIX}-zlib")
checkdepends=("${MINGW_PACKAGE_PREFIX}-openshadinglanguage")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: Python bindings")
source=(https://github.com/AcademySoftwareFoundation/OpenColorIO/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        0003-fix-python-sitearch.patch
        0005-fix-ifstream-arg.patch
        0006-fix-neon-build.patch)
sha256sums=('2d8f2c47c40476d6e8cea9d878f6601d04f6d5642b47018eaafa9e9f833f3690'
            '4bb650747906aab5b71579d05674ee59a811d6370a6dcda93b5880abb094be70'
            'd2c3b20715ade9cf7599967e8cdd8688ad71e4956fa1825c9025ff862e8bcb37'
            '78260b6d62c7f05a38e940b41c5d07f7e9d8f418a6e5fa64ba27d5237a936944')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-${pkgver}

  # https://github.com/AcademySoftwareFoundation/OpenColorIO/issues/2136
  # https://github.com/AcademySoftwareFoundation/OpenColorIO/issues/2137
  apply_patch_with_msg \
    0003-fix-python-sitearch.patch \
    0005-fix-ifstream-arg.patch \
    0006-fix-neon-build.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
       ${extra_config} \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DOCIO_BUILD_TESTS=OFF \
      -DOCIO_BUILD_GPU_TESTS=OFF \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OpenColorIO/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done
}
